package com.xgj.utils;

import java.util.HashMap;
import java.util.Map;

//测试类
public class TestHttpUtil {

    public static void main(String[] args) {
        try {
            // 测试 GET 请求
//            testGetRequest();
            testPostRequest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试 GET 请求
     */
    private static void testGetRequest() throws Exception {
        // 接口地址
        String url = "https://randomuser.me/api/?results=5000";

        // 可选：设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent", "Mozilla/5.0");
        headers.put("Accept-Language", "en-US");

        // 发送 GET 请求
        String response = HttpUtil.get(url, headers);

        // 打印响应结果
        System.out.println("GET 请求响应：");
        System.out.println("响应长度: " + response.length());
        System.out.println("响应内容（前500字符）: " + response.substring(0, Math.min(500, response.length())));
    }

  /**
     * 测试 POST 请求（使用公共测试接口）
     */
    private static void testPostRequest() throws Exception {
        // 使用 httpbin.org 的测试接口
        String url = "https://httpbin.org/post";

        // 设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("X-Custom-Header", "TechminoTest");

        // 构造测试请求体（JSON格式）
        String requestBody = "{"
                + "\"game\": \"Techmino\","
                + "\"action\": \"rotate\","
                + "\"timestamp\": " + System.currentTimeMillis() + ","
                + "\"isTest\": true"
                + "}";

        // 发送 POST 请求
        String response = HttpUtil.post(url, headers, requestBody);

        // 打印响应结果
        System.out.println("POST 请求响应：");
        System.out.println("响应长度: " + response.length());
        System.out.println("响应内容（前500字符）: " + response.substring(0, Math.min(500, response.length())));
    }

    private static void testPostRequest1() throws Exception {
        String url = "http://192.168.1.240:6060/eai/login/login";

        // 设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("X-Custom-Header", "TechminoTest");

        // 构造新的请求体（JSON格式）
        String requestBody = "{"
                + "\"params\": {"
                + "\"corpid\": \"e0149951\","
                + "\"secret\": \"eeaee1ce415f573319459c53\""
                + "}"
                + "}";

        // 发送 POST 请求
        String response = HttpUtil.post(url, headers, requestBody);

        // 打印响应结果
        System.out.println("POST 请求响应：");
        System.out.println("响应长度: " + response.length());
        System.out.println("响应内容（前500字符）: " +
                (response.length() > 0 ? response.substring(0, Math.min(500, response.length())) : "空响应"));
    }
}
